package com.baidumap.administrator.happystudy.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.base.Operation;
import com.ab.util.SharedPreferencesHelper;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.model.Item_ViewPagerGridModel_Teacher;
import com.baidumap.administrator.happystudy.utils.GlideUtil;

import java.util.List;

/**
 * 描述：
 * 作者：xiaoxing on 17/4/10 23:13
 * 邮箱：2235445233@qq.com
 */
public class Adapter_GridView_ShouYe_Teacher extends BaseAdapter {
    private List<Item_ViewPagerGridModel_Teacher> mDatas;
    private LayoutInflater inflater;
    /**
     * 页数下标,从0开始(当前是第几页)
     */
    private int curIndex;
    /**
     * 每一页显示的个数
     */
    private int pageSize;

    private Context mContext;
    private Fragment mFragment;
    private SharedPreferencesHelper sHelper;
    private Operation mOperation;

    public Adapter_GridView_ShouYe_Teacher(Context context, Fragment fragment, List<Item_ViewPagerGridModel_Teacher> mDatas, int curIndex, int pageSize, SharedPreferencesHelper sHelper, Operation operation) {
        this.mContext = context;
        this.mFragment = fragment;
        inflater = LayoutInflater.from(context);
        this.mDatas = mDatas;
        this.curIndex = curIndex;
        this.pageSize = pageSize;
        this.sHelper = sHelper;
        this.mOperation = operation;
    }

    /**
     * 先判断数据集的大小是否足够显示满本页,如果够，则直接返回每一页显示的最大条目个数pageSize,如果不够，则有几项就返回几,(也就是最后一页的时候就显示剩余item)
     */
    @Override
    public int getCount() {
        return mDatas.size() > (curIndex + 1) * pageSize ? pageSize : (mDatas.size() - curIndex * pageSize);

    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position + curIndex * pageSize);
    }

    @Override
    public long getItemId(int position) {
        return position + curIndex * pageSize;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_teacher_list_card, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.card_img = (ImageView) convertView.findViewById(R.id.card_img);
            viewHolder.card_name = (TextView) convertView.findViewById(R.id.card_name);
            viewHolder.card_name1 = (TextView) convertView.findViewById(R.id.card_name1);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        /**
         * 在给View绑定显示的数据时，计算正确的position = position + curIndex * pageSize
         */
        final int pos = position + curIndex * pageSize;
        viewHolder.card_name.setText(mDatas.get(pos).getName() == null ? "新老师" : mDatas.get(pos).getName());
        viewHolder.card_name1.setText(mDatas.get(pos).getType() == null ? "核实中" : mDatas.get(pos).getType());

        GlideUtil.displayTeacherImg(mContext, mDatas.get(pos).getPic(), viewHolder.card_img);

        return convertView;
    }


    class ViewHolder {
        private ImageView card_img;
        private TextView card_name;
        private TextView card_name1;
    }
}